package com.kvest.testtask.activity;

import com.kvest.testtask.R;
import com.kvest.testtask.core.SitesManager;
import com.kvest.testtask.datamodel.Site;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class AddSiteActivity extends Activity
{
	private static final int NO_ID_DIALOG = 0;
	private static final int REPLACE_CONFIRMATION_DIALOG = 1;
	
	private EditText feature_id;
	private EditText county_name;
	private EditText description; 
	private EditText feat_class;
	private EditText fips_class;
	private EditText fips_county_cd;
	private EditText full_county_name;
	private EditText link_title;
	private EditText url;
	private EditText name;
	private EditText latitude;
	private EditText longitude;
	private EditText state_abbreviation;
	private EditText state_name;
	
	private Site site = null;;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_site_layout);
        
        setComponents();
        setButtonsListener();
    }
	
	@Override
    protected Dialog onCreateDialog(int id) 
    {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
        switch (id) 
        {
        	case NO_ID_DIALOG : 
        		builder.setTitle(R.string.info);
        		builder.setIcon(android.R.drawable.ic_dialog_alert);
        		builder.setMessage(R.string.no_id_message); 
        		builder.setNegativeButton(android.R.string.ok, null);
        		break;
        	case REPLACE_CONFIRMATION_DIALOG : 
        		builder.setTitle(R.string.confirmation);
        		builder.setIcon(android.R.drawable.ic_dialog_info);
        		builder.setMessage(R.string.replace_confirmation); 
        		builder.setNegativeButton(android.R.string.no, null);
        		builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() 
        		{	
					@Override
					public void onClick(DialogInterface dialog, int which) 
					{
						SitesManager.getManager().addSite(site);
						site = null;
						
						AddSiteActivity.this.finish();
					}
				});
        		break;
        }
        
        return builder.create();
    }
	
	private void setComponents()
	{
		feature_id = (EditText)findViewById(R.id.edt_feature_id);
		county_name = (EditText)findViewById(R.id.edt_county_name);
		description = (EditText)findViewById(R.id.edt_description); 
		feat_class = (EditText)findViewById(R.id.edt_feat_class);
		fips_class = (EditText)findViewById(R.id.edt_fips_class);
		fips_county_cd = (EditText)findViewById(R.id.edt_fips_county_cd);
		full_county_name = (EditText)findViewById(R.id.edt_full_county_name);
		link_title = (EditText)findViewById(R.id.edt_link_title);
		url = (EditText)findViewById(R.id.edt_url);
		name = (EditText)findViewById(R.id.edt_name);
		latitude = (EditText)findViewById(R.id.edt_latitude);
		longitude = (EditText)findViewById(R.id.edt_longitude);
		state_abbreviation = (EditText)findViewById(R.id.edt_state_abbreviation);
		state_name = (EditText)findViewById(R.id.edt_state_name);
	}
	
	private void setButtonsListener()
	{
		findViewById(R.id.save_btn).setOnClickListener(new View.OnClickListener() 
        {	
			public void onClick(View v) 
			{
				if (feature_id.getText().toString().equals(""))
				{
					showDialog(NO_ID_DIALOG);
				}
				else
				{
					addSite();
				}
			}
		});
        
        findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() 
        {	
			public void onClick(View v) 
			{	
				AddSiteActivity.this.finish();
			}
		});
	}
	
	private void addSite()
	{
		//Create Site object
		site = new Site(Integer.parseInt(feature_id.getText().toString()));
		site.setCountyName(county_name.getText().toString());
		site.setDescription(description.getText().toString());
		site.setFeatClass(feat_class.getText().toString());
		site.setFipsClass(fips_class.getText().toString());
		try
		{
			site.setFipsCountyCd(Integer.parseInt(fips_county_cd.getText().toString()));
		}catch(Exception ex){};
		site.setFullCountyName(full_county_name.getText().toString());
		site.setLinkTitle(link_title.getText().toString());
		site.setUrl(url.getText().toString());
		site.setName(name.getText().toString());
		try
		{
			site.setPrimaryLatitude(Float.parseFloat(latitude.getText().toString()));
		}catch(Exception ex){};
		try
		{
			site.setPrimaryLongitude(Float.parseFloat(longitude.getText().toString()));
		}catch(Exception ex){};
		site.setStateAbbreviation(state_abbreviation.getText().toString());
		site.setStateName(state_name.getText().toString());
		
		if (SitesManager.getManager().hasSite(site.getFeatureId()))
		{
			//ask to replace
			showDialog(REPLACE_CONFIRMATION_DIALOG);
		}
		else
		{
			//add site object to manager
			SitesManager.getManager().addSite(site);
			site = null;
			
			AddSiteActivity.this.finish();
		}
	}
}
